//
// Created by ASUS on 2025/4/23.
//

#include "WorkManager.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include"Worker.h"
#include<fstream>
#define FILENAME "emp.txt"
WorkManager::WorkManager(){
    //file not exist
    ifstream ifs;
    ifs.open(FILENAME,ios::in);
    if(!ifs.is_open()){
        cout<<"file not exist"<<endl;
        //init
        this->num=0;
        this->pWorker=NULL;
        this->File_Exist=false;
        ifs.close();
        return ;
    }

    //file exist but empty
    char ch;
    ifs>>ch;
    if(ifs.eof()){
        cout<<"file is empty"<<endl;
        this->num=0;
        this->pWorker=NULL;
        this->File_Exist= false;
        ifs.close();
        return ;
    }

    //file exist and not empty
    ifs.close();
    int cnt=Get_File_Cnt();
//    cout<<"cnt="<<cnt<<endl;
    this->num=cnt;
    this->File_Exist=true;

    //bulid the space
    this->pWorker=new Worker*[this->num];
    this->Init_Workers();

    //测试代码
//    for (int i = 0; i < this->num; i++) {
//        cout<<"id:"<<this->pWorker[i]->id<<" "
//             <<"name:"<<this->pWorker[i]->name<<" "
//             <<"deptId:"<<this->pWorker[i]->deptId<<endl;
//    }




}

void WorkManager::Init_Workers() {
    ifstream ifs;
    ifs.open(FILENAME,ios::in);
    int id;
    string name;
    int deptId;
    int cntt=0;
    while(ifs>>id>>name>>deptId){
        Worker * worker=NULL;
        if(deptId==1){
            worker=new employee(id,name,deptId);
        }
        else if(deptId==2){
            worker=new manager(id,name,deptId);
        }
        else {
            worker=new boss(id,name,deptId);
        }
        this->pWorker[cntt]=worker;
        cntt++;
    }
    ifs.close();
}

int WorkManager::Get_File_Cnt() {
    ifstream ifs;
    ifs.open(FILENAME,ios::in);
    int cnt=0;

    int id;
    string name;
    int deptId;
    while(ifs>>id>>name>>deptId){
        cnt++;
    }
    ifs.close();
    return cnt;

}



void WorkManager::Show_Menu(){
    cout<<"**********************************"<<endl;
    cout<<"******  welcome to ***************"<<endl;
    cout<<"******  0.exit     **************"<<endl;
    cout<<"******  1.add      ***************"<<endl;
    cout<<"******  2.show all ***************"<<endl;
    cout<<"******  3.delete   ***************"<<endl;
    cout<<"******  4.change   ***************"<<endl;
    cout<<"******  5.find     ***************"<<endl;
    cout<<"******  6.sort     ***************"<<endl;
    cout<<"******  7.cls      ***************"<<endl;
    cout<<"**********************************"<<endl;
    cout<<endl;
};

void WorkManager::ExitSystem(){
    cout<<"welcome to use again";
    system("pause");
    exit(0);
};

void WorkManager::Add_Worker() {
    cout<<"plesse input the number of worker you want to add"<<endl;
    int addnum=0;
    cin>>addnum;//record the number of worker you want to add
    if(addnum>=0){
        //claculate the number of worker
        int newSize=this->num+addnum;

        //bulid a new array
        Worker **newSpace=new Worker*[newSize];

        //copy the old array to the new array
        if(this->num!=0){
            for(int i=0;i<this->num;i++){
                newSpace[i]=this->pWorker[i];
            }
        }

        //add the new worker to the new array
        for(int i=0;i<addnum;i++){
            int id=0;//id
            string name="";//name
            int dSelect=0;//department

            cout<<"please input the id of new worker"<<i+1<<endl;
            while(true){
                cin>>id;
                if(this->IsExist(id)!= -1){
                    cout<<"the id is exist,please input again"<<endl;
                }
                else{
                    break;
                }
            }

            cout<<"please input the name of new worker"<<i+1<<endl;
            cin>>name;

            cout<<"please input the department of new worker"<<i+1<<endl;
            cout<<"1.employee"<<endl;
            cout<<"2.manager"<<endl;
            cout<<"3.boss"<<endl;
            cin>>dSelect;



            Worker *newWorker=NULL;
            switch (dSelect) {
                case 1:
                    newWorker=new employee(id,name,dSelect);
                    break;
                case 2:
                    newWorker=new manager(id,name,dSelect);
                    break;
                case 3:
                    newWorker=new boss(id,name,dSelect);
                    break;
                    default:
                        break;
            }
            //add the new worker to the new array
            newSpace[this->num+i]=newWorker;

        }

        //free the old array
        delete[] this->pWorker;

        //change the pointer to the new array
        this->pWorker=newSpace;

        //change the number of workers
        this->num=newSize;
        this->File_Exist=true;

        //save the workers in file
        this->Save_File();

        //tips
        cout<<"add success "<<addnum<<" workers"<<endl;
    }
    else{
        cout<<"input error"<<endl;
    }

    //CLS
    system("pause");
    system("cls");
}


void WorkManager::Save_File(){
    ofstream ofs;
    ofs.open(FILENAME,ios::out);//open file
    if(!ofs.is_open()){
        cout<<"open file error"<<endl;
        return;
    }


    for(int i=0;i<this->num;i++){
        ofs<<this->pWorker[i]->id<<" ";
        ofs<<this->pWorker[i]->name<<" ";
        ofs<<this->pWorker[i]->deptId<<" "<<endl;
    }
    ofs.close();

}


void WorkManager::Show_Workers() {
        //judge whether the file exists
        if(this->File_Exist==false){
            cout<<"file not exist"<<endl;
        }
        else{
            for(int i=0;i<this->num;i++){
                //show info
                this->pWorker[i]->ShowTnfo();
            }
        }

        //CLS
        system("pause");
        system("cls");
}

void WorkManager::Del_Worker() {
    if(this->File_Exist==false){
        cout<<"file not exist"<<endl;
    }
    else{
        //input id
        int id;
        cout<<"input id of you want to delate"<<endl;
        cin>>id;
        int index=this->IsExist(id);
        if(index!=-1){
            for(int i=index;i<this->num-1;i++){
                this->pWorker[i]=this->pWorker[i+1];
            }
            this->num--;
            if(this->num==0){
                this->File_Exist=false;
            }

            this->Save_File();
            cout << "delete success" << endl;

        }
        else{
            cout<<"not exist this id"<<endl;
        }
    }

    //CLS
    system("pause");
    system("cls");

}

int WorkManager::IsExist(int id) {
    int index=-1;//default not exist
    for(int i=0;i<this->num;i++){
        if(this->pWorker[i]->id==id){
            index=i;
            break;
        }
    }

    return index;
}

void WorkManager::Change_Worker() {
    if(!this->File_Exist){
        cout<<"file not exist"<<endl;
    }
    else{
        //input id
        int id;
        cout<<"input id of you want to change"<<endl;
        cin>>id;
        int index=this->IsExist(id);
        if(index!=-1){
            cout<<"id:"<<this->pWorker[index]->id
                <<"\tname:"<<this->pWorker[index]->name
                <<"\tdept:"<<this->pWorker[index]->getDeptName()<<endl;
            delete this->pWorker[index];

            int newId;
            string newName;
            int newDeptId;
            cout<<"input new id"<<endl;
            while(true){
                cin>>newId;
                if(this->IsExist(newId)!=-1){
                    cout<<"the id is exist,please input again"<<endl;
                }
                else{
                    break;
                }
            }

            cout<<"input new name"<<endl;
            cin>>newName;
            cout<<"input new dept id"<<endl;
            cout<<"1.employee"<<endl;
            cout<<"2.manager"<<endl;
            cout<<"3.boss"<<endl;
            cin>>newDeptId;
            Worker *worker=NULL;
            if(newDeptId==1){
                worker=new employee(newId,newName,newDeptId);
            }
            else if(newDeptId==2){
                worker=new manager(newId,newName,newDeptId);
            }
            else{
                worker=new boss(newId,newName,newDeptId);;
            }

            this->pWorker[index]=worker;
            cout<<"change success"<<endl;
            this->Save_File();
        }
        else{
            cout<<"not exist this id"<<endl;
        }
    }

    //CLS
    system("pause");
    system("cls");

}

void WorkManager::Find_Worker() {
    if(this->File_Exist==false){
        cout<<"file not exist"<<endl;
    }
    else{
        cout<<"input the fun of you want"<<endl;
        cout<<"1.id"<<endl;
        cout<<"2.name"<<endl;
        int choice;
        cin>>choice;
        if(choice==1){
            cout<<"input id"<<endl;
            int id;
            cin>>id;
            int index=this->IsExist(id);
            if(index!=-1){
                this->pWorker[index]->ShowTnfo();
            }
            else{
                cout<<"not exist this id"<<endl;
            }
        }
        else if(choice==2){
            cout<<"input name"<<endl;
            string name;
            cin>>name;
            bool flag=false;
            for(int i=0;i<this->num;i++){
                if(this->pWorker[i]->name==name){
                    this->pWorker[i]->ShowTnfo();
                    flag=true;
                    break;
                }
            }
            if(flag==false){
                cout<<"not exist this name"<<endl;
            }

        }
        else{
            cout<<"input error"<<endl;
        }
    }

    system("pause");
    system("cls");
}

void WorkManager::Sort_Worker() {
    if(this->File_Exist==false){
        cout<<"file not exist"<<endl;
    }
    else{
        cout<<"input the fun of you want"<<endl;
        cout<<"1.asc"<<endl;
        cout<<"2.dsc"<<endl;
        int choice;
        cin>>choice;
        if(choice==1) {
            for(int i=0;i<this->num-1;i++){
                for(int j=i+1;j<this->num;j++){
                    if(this->pWorker[i]->id>this->pWorker[j]->id){
                        swap(this->pWorker[i],this->pWorker[j]);
                    }
                }
            }
            cout<<"sort success"<<endl;
            cout<<"the new worker list:"<<endl;
            this->Show_Workers();

        }
        else if(choice==2) {
            for (int i = 0; i < this->num - 1; i++) {
                for (int j = i + 1; j < this->num; j++) {
                    if (this->pWorker[i]->id < this->pWorker[j]->id) {
                        swap(this->pWorker[i], this->pWorker[j]);
                    }
                }
            }

            cout<<"sort success"<<endl;
            cout<<"the new worker list:"<<endl;
            this->Show_Workers();
        }
        else{
            cout<<"error"<<endl;
        }
    }

    system("pause");
    system("cls");
}


void WorkManager::Cls_Worker() {
    cout<<"you sure to cls the worker list?(y/n)"<<endl;
    char choice;
    cin>>choice;
    if(choice=='y'){
        ofstream ofs;
        ofs.open(FILENAME,ios::trunc);
        ofs.close();

        //delete the worker in the memory
        if(this->pWorker!=NULL){
            for(int i=0;i<this->num;i++){
                delete this->pWorker[i];
                this->pWorker[i]=NULL;
            }

            //delete the pointer
            delete [] this->pWorker;
            this->pWorker=NULL;
            this->num=0;
            this->File_Exist=false;
        }


        cout<<"cls success"<<endl;
    }
    else if(choice=='n'){
        cout<<"tanks for your use"<<endl;
    }
    else{
        cout<<"error input"<<endl;
    }
    system("pause");
    system("cls");
}

WorkManager::~WorkManager(){
    if(this->pWorker!=NULL){
        for(int i=0;i<this->num;i++){
            delete this->pWorker[i];
            this->pWorker[i]=NULL;
        }

        //delete the pointer
        delete [] this->pWorker;
        this->pWorker=NULL;
        this->num=0;
        this->File_Exist=false;
    }
}





